package bshark.web.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import bshark.entity.system.Role;
import bshark.entity.system.User;
import bshark.service.system.UserService;
import com.web.page.Page;
import com.web.struts2.CRUDSupportAction;

/**
 * 用户管理Action.
 * @see CRUDActionSupport
 * @author bshark
 */
@ParentPackage("default")
@Results( { @Result(name = "reload", location = "user.shtml", type = "redirect") })
public class UserAction extends CRUDSupportAction {

	private static final long serialVersionUID = -2180690009159324387L;
	@Autowired
	private UserService userService;
	private Page<User> page = new Page<User>(10);// 每页10条记录
	private User user;
	private List<Role> allRoles;
	private String orderBy;
	private String order;

	@Override
	protected void prepareModel() throws Exception {
		if (user != null && StringUtils.isNotBlank(user.getId())) {
			user = userService.getUser(user.getId());
		} else {
			if (StringUtils.isNotBlank(getId())) {
				user = userService.getUser(getId());
				ids = user.getRoleIds();
			} else {
				user = new User();
			}
		}
	}

	@Override
	public String list() throws Exception {
		if (StringUtils.isNotBlank(getOrderBy())
				&& StringUtils.isNotBlank(getOrder())) {
			page.setOrderBy(getOrderBy());
			page.setOrder(getOrder());
		}
		page = userService.getAllUsers(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		allRoles = userService.getAllRole();
		return INPUT;
	}

	@SuppressWarnings("finally")
	@Override
	public String save() throws Exception {
		try {
			userService.saveUser(user, ids);
			addActionMessage("保存用户成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存用户失败!");
		} finally {
			return RELOAD;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public String delete() throws Exception {
		try {
			userService.deleteUser(getId());
			addActionMessage("删除用户成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("删除用户失败!");
		} finally {
			return RELOAD;
		}
	}

	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkLoginName() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String loginName = request.getParameter("loginName");
		String orgLoginName = request.getParameter("orgLoginName");

		if (userService.isLoginNameUnique(loginName, orgLoginName))
			return renderText("true");
		else
			return renderText("false");
	}

	public Page<User> getPage() {
		return page;
	}

	public void setPage(Page<User> page) {
		this.page = page;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public void setAllRoles(List<Role> allRoles) {
		this.allRoles = allRoles;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

}
